package infopreserve.main.mobile;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
	SurfaceHolder mHolder;
	public Camera camera;

	public CameraView(Context context) {
		super(context);
		mHolder = getHolder();
		mHolder.addCallback(this);
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
	
	public void surfaceCreated(SurfaceHolder holder) {
			camera = Camera.open();
			camera.setDisplayOrientation(90);
			try {
				camera.setPreviewDisplay(holder);
				camera.setPreviewCallback(new PreviewCallback() {
					public void onPreviewFrame(byte[] data, Camera camera) {
						CameraView.this.invalidate();
					}
				});
			} catch (IOException e) {
				e.printStackTrace();
			}

	}

	  public void surfaceDestroyed(SurfaceHolder holder) {
		camera.stopPreview();
	    camera.release();
	    camera = null;
	  }
	  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		if(camera != null)
	    camera.startPreview();
	  }
	}